package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @User: Yr
 * @Date: 2021/12/13
 * @Description: 商城首页显示
 */
@Anoymous
@RestController
@RequestMapping("/shopping")
public class HomeController {

    @Reference(check = false)
    IProductService iProductService;
    @Reference(check = false)
    IHomeService iHomeService;
    @Reference(check = false)
    IProductCateService iProductCateService;
    @Reference(check = false)
    IContentService iContentService;

    /**
     * 首页显示
     * @return
     */
    @GetMapping("homepage")
    @Anoymous
    public ResponseData getHomepage() {
        HomePageResponse response = iHomeService.homepage();
        if (!ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response.getPanelContentItemDtos());
    }


    /**
     * 获取导航栏信息
     * @return
     */
    @GetMapping("navigation")
    @Anoymous
    public ResponseData getNavigation() {
        NavListResponse response = iContentService.queryNavList();
        if (!ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response.getPannelContentDtos());
    }


    /**
     * 获取全部商品品类
     *
     * @return
     */
    @GetMapping("categories")
    @Anoymous
    public ResponseData getAllProductCate() {
        AllProductCateRequest request = new AllProductCateRequest();
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response.getProductCateDtoList());
    }

    /**
     * 获取指定id的商品信息
     */
    @GetMapping("/product/{id}")
    @Anoymous
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = iProductService.getProductDetail(request);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response.getProductDetailDto());
    }
    @Anoymous
    @GetMapping("goods")
    public ResponseData getAllProduct(AllProductRequest request) {

        AllProductResponse response = iProductService.getAllProduct(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response, "success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    @Anoymous
    @GetMapping("recommend")
    public ResponseData getRecommendGoods() {

        RecommendResponse response = iProductService.getRecommendGoods();

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response, "success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


}
